package com.bestv.datamodeler;

import com.bestv.mahout.model.ModelData;
import com.bestv.mahout.model.cf.impl.MostSimilarModelTrainer;
import com.bestv.utils.common.StringIdMap;
import com.bestv.utils.config.GlobalSettings;
import com.bestv.utils.database.DBConnectionManager;
import com.bestv.utils.database.JDBCHelper;

import java.sql.Connection;
import java.util.ArrayList;

/**
 * 协同过滤推荐计算入口
 * User: Henry
 * Date: 14-5-7
 */
public class CFModeler {
    public static void main(String[] args) {
        ModelData data = new ModelData();
        data.setTrainDataFolder(GlobalSettings.getInstance().getProgramCFTrainDataFolder());
        data.setTrainFilePattern("scoredata_movie\\..*");
        data.setTrainTimeWindow(30 * 24 * 60 * 60);
        data.setDelimiterRegex("\\|");
        data.setStringId(true);

        MostSimilarModelTrainer trainer = new MostSimilarModelTrainer();
        trainer.setDataModel(data.getDataModel());
        trainer.setModelResultTableName("programmostsimilar_movie");

        String sql;
        ArrayList<Object[]> rs;
        Connection conn = DBConnectionManager.getMasterConnection();

        StringIdMap programIdMap = new StringIdMap();
        sql = "select ProgramId from program";
        rs = JDBCHelper.executeQuery(conn, sql);
        programIdMap.initialize(rs);
        trainer.setItemIdMap(programIdMap);
        trainer.setStringId(true);

        trainer.trainModel();
        trainer.saveSimilarMatrixToDB();

        ModelData data2 = new ModelData();
        data2.setTrainDataFolder(GlobalSettings.getInstance().getSeriesCFTrainDataFolder());
        data2.setTrainFilePattern("scoredata_drama\\..*");
        data2.setTrainTimeWindow(30 * 24 * 60 * 60);
        data2.setDelimiterRegex("\\|");
        data2.setStringId(true);

        MostSimilarModelTrainer trainer2 = new MostSimilarModelTrainer();
        trainer2.setDataModel(data2.getDataModel());
        trainer2.setModelResultTableName("seriesmostsimilar_drama");

        StringIdMap seriesIdMap = new StringIdMap();
        sql = "select SeriesId from series";
        rs = JDBCHelper.executeQuery(conn, sql);
        seriesIdMap.initialize(rs);
        trainer2.setItemIdMap(seriesIdMap);
        trainer2.setStringId(true);

        trainer2.trainModel();
        trainer2.saveSimilarMatrixToDB();
    }
}
